package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrderDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author: qingtao
 * @Date 2022/3/23 18:02
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired(required = false)
    private OrderService orderService;

    @Autowired(required = false)
    private HttpSession session;

    /**
     * 作用： 下单
     * @param orders
     * @param session
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders, HttpSession session){
        //1.得到登陆者
        Long userId = (Long) session.getAttribute("user");
        //下单
        orderService.submit(orders,userId);
        return R.success("下单成功");
    }

    @GetMapping("/page")
    public R<Page<OrderDto>> findByPage(Integer page, Integer pageSize,Long id){
        Page<OrderDto> ordersPage = orderService.findByUserPage(page, pageSize,null,id);
        return R.success(ordersPage);
    }

    @GetMapping("/userPage")
    public R<Page<OrderDto>> findByUserPage(Integer page, Integer pageSize){
        Long userId = (Long) session.getAttribute("user");
        Page<OrderDto> ordersPage = orderService.findByUserPage(page, pageSize, userId,null);
        return R.success(ordersPage);
    }
}
